package Student;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class number1 {
	public static void main(String[] args) throws Exception {
		String xingStr="https://www.jimingbao.com/90981.html";
		String mingStr="http://www.haoming8.cn/mingzi/8785.html";
		
		String webCrawlerXing = webCrawler(xingStr);
		String webCrawlerMing = webCrawler(mingStr);
//		System.out.println(webCrawlerXing);
		
		
		
		//通过正则表达式，把其中符号要求的数据获取出来
		ArrayList<String> dataXing = getData(webCrawlerXing,"(.、){2}");
		ArrayList<String> dataMing = getData(webCrawlerMing,"(..、){4}..");
//		System.out.println(dataXing);
//		System.out.println(dataMing);
		//处理姓氏
		ArrayList<String> xingList=new ArrayList<String>();
		for (String xing : dataXing) {
			//李、王、
			String[] split = xing.split("、");
			for(int i=0;i<split.length;i++){
				xingList.add(split[i]);
			}
		}
//		System.out.println(xingList);
		//处理名字
		ArrayList<String> mingList=new ArrayList<String>();
		for (String ming : dataMing) {
			//哲睿、益昌、夏明、胜才、浚龙
			String[] split = ming.split("、");
			for(int i=0;i<split.length;i++){
				mingList.add(split[i]);
			}
		}
//		System.out.println(mingList);
		
		//自由生成名字
		int key = 0;
		Random numbRandom = new Random();
		ArrayList<String> infos = getInfos(xingList,mingList,100);
		List<Student> studentList = new ArrayList<>();
		for (String string : infos) {
		if(key<50) {
			int boy = numbRandom.nextInt(6) + 20;
			Student student = new Student(string,"男",boy);
			studentList.add(student);
			key++;
		}else {
			int girl = numbRandom.nextInt(5) + 18;
			Student student = new Student(string,"女",girl);
			studentList.add(student);
		}
		}	
//		System.out.println(infos);
		
		//写出数据
		int num = 1;
		BufferedWriter bw=new BufferedWriter(new FileWriter("names.txt"));
		for (Student student : studentList) {
           if(num<=50) {
        	   bw.write(student.getName()+"-"+student.getGender()+"-"+student.getAge());
               num++;
               bw.write("\n");
           }else {
        	   bw.write(student.getName()+"-"+student.getGender()+"-"+student.getAge());
               num++;
               bw.write("\n");
           }
        }
		bw.close();
		System.out.println("写入成功");
	}
	private static ArrayList<String> getInfos(ArrayList<String> xingList, ArrayList<String> mingList, int count) {
		HashSet<String> set=new HashSet<>();
		while(true){
			if(set.size()==count){
				break;
			}
			//随机
			Collections.shuffle(mingList);
			Collections.shuffle(xingList);
			set.add(xingList.get(0)+mingList.get(0));
		}
		ArrayList<String> list=new ArrayList<>(set);
		return list;
	}
	public static ArrayList<String> getData(String str,String regex){
		ArrayList<String> list=new ArrayList<>();
		//按照正则表达式的规则，去获取数据
		Pattern pattern=Pattern.compile(regex);
		//按照pattern规则，到str中获取数据
		Matcher matcher = pattern.matcher(str);
		while(matcher.find()){
			String group = matcher.group();
//			System.out.println(group);
			list.add(group);
		}
		return list;
	}
	
	/*
	 * 作用：
	 *  从网络中爬取数据，把数据拼接成字符串返回
	 *  参数：网址
	 *  返回值：爬取到的所有的数据
	 */
	public static String webCrawler(String net) throws Exception{
		StringBuilder sb=new StringBuilder();
		//创建一个url对象
		URL url=new URL(net);
		//连接上这个网址 ，保证网络是畅通的
		URLConnection conn = url.openConnection();
		//读取数据
		InputStreamReader isr=new InputStreamReader(conn.getInputStream(),"utf-8");
		int ch;
		while((ch=isr.read())!=-1){
			sb.append((char)ch);
		}
		return sb.toString();
	}
}
